package scenic.toolkit;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.util.Vector;

import scenic.geom.Double2;

class RootMouseHandler implements MouseListener, MouseMotionListener,
MouseWheelListener {
	private RootPane root;
	private Vector<ComponentView> hierarchy = new Vector<ComponentView>();
	
	RootMouseHandler(RootPane root) {
		this.root = root;
	}
	
	private void updateHierarchy(Vector<ComponentView> nh, MouseEvent e) {
		int i;
		Double2 p = new Double2(e.getX(), e.getY());
		
		for(i = 0; i < Math.min(hierarchy.size(), nh.size()); i++)
			if(hierarchy.elementAt(i) != nh.elementAt(i))
				break;
		
		for(int j = hierarchy.size() - 1; j >= i; j--)
		{
			ComponentView c = hierarchy.elementAt(j);
			Double2 p2 = c.getTotalTransform().inverse().multiply(p);
			Vector<scenic.toolkit.event.MouseListener> listeners = c.getMouseListeners();
			
			scenic.toolkit.event.MouseEvent te = 
				new scenic.toolkit.event.MouseEvent(c, p2, 0, e.getButton());
			
			for(scenic.toolkit.event.MouseListener l : listeners)
				l.mouseExited(te);
		}
		
		for(int j = i; j < nh.size(); j++)
		{
			ComponentView c = nh.elementAt(j);
			Double2 p2 = c.getTotalTransform().inverse().multiply(p);
			Vector<scenic.toolkit.event.MouseListener> listeners = c.getMouseListeners();
			
			scenic.toolkit.event.MouseEvent te = 
				new scenic.toolkit.event.MouseEvent(c, p2, 0, e.getButton());
			
			for(scenic.toolkit.event.MouseListener l : listeners)
				l.mouseEntered(te);
		}
		
		hierarchy = nh;
	}
	
	private Vector<ComponentView> getHierarchy(Double2 p) {
		Vector<ComponentView> h = new Vector<ComponentView>();		
		ComponentView c = root.getContent();
		ComponentView child;
		
		h.add(c);
		while((child = c.getChildAt(p)) != null) {
			c = child;
			p = c.getTransform().inverse().multiply(p);
			h.add(c);
		}
		
		return h;
	}
	
	public void mouseDragged(MouseEvent e) {
	}

	public void mouseMoved(MouseEvent e) {
		Double2 p = new Double2(e.getX(), e.getY());
		
		Vector<ComponentView> h = getHierarchy(p);
		
		updateHierarchy(h, e);
	}

	public void mouseClicked(MouseEvent e) {
		Double2 p = new Double2(e.getX(), e.getY());
		ComponentView c = root.getContent();
		ComponentView child;
		
		while((child = c.getChildAt(p)) != null) {
			c = child;
			p = c.getTransform().inverse().multiply(p);
		}
		
		scenic.toolkit.event.MouseEvent te;
		
		te = new scenic.toolkit.event.MouseEvent(c, p, 0, e.getButton());
		
		Vector<scenic.toolkit.event.MouseListener> listeners = c.getMouseListeners();
		
		for(scenic.toolkit.event.MouseListener l : listeners)
			l.mouseClicked(te);
	}

	public void mouseEntered(MouseEvent e) {
	}

	public void mouseExited(MouseEvent e) {
	}

	public void mousePressed(MouseEvent e) {
	}

	public void mouseReleased(MouseEvent e) {
	}

	public void mouseWheelMoved(MouseWheelEvent e) {
	}
}
